package com.dhcc.DRGsHosp.facade.impl;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.dayatang.utils.Page;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;

import com.dhcc.DRGsHosp.application.DailyRecordApplication;
import com.dhcc.DRGsHosp.application.DepartmentApplication;
import com.dhcc.DRGsHosp.application.XmlStructureApplication;
import com.dhcc.DRGsHosp.core.domain.DailyRecord;
import com.dhcc.DRGsHosp.core.domain.Department;
import com.dhcc.DRGsHosp.facade.DayMonitorFacade;
import com.dhcc.DRGsHosp.facade.dto.DailyRecordDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.DailyRecordAssembler;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;
import com.dhcc.DRGsHosp.infra.util.GlobalData;
import com.dhcc.DRGsHosp.infra.util.StringUtils;
@Named  //--用于测试类的调用
public class DayMonitorFacadeImpl implements DayMonitorFacade{
	
	private static final Logger logger = LoggerFactory.getLogger(DayMonitorFacadeImpl.class);

	@Autowired
	private DailyRecordApplication  application;
	
	@Autowired
	private XmlStructureApplication xmlStructureApplication;
	
	@Autowired
	private DepartmentApplication departmentApplication;
	
	
	//private SendMessageFacadeImpl sendMessageFacadeImpl = new SendMessageFacadeImpl();
	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	public Page<DailyRecordDTO> pageQueryDailyRecord(DailyRecordDTO queryVo, int currentPage, int pageSize) {
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _dailyRecord from DailyRecord _dailyRecord   where 1=1 ");
	   	if (queryVo.getDrEpisodeid() != null && !"".equals(queryVo.getDrEpisodeid())) {
	   		jpql.append(" and _dailyRecord.drEpisodeid like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrEpisodeid()));
	   	}		
	   	if (queryVo.getDrMrno() != null && !"".equals(queryVo.getDrMrno())) {
	   		jpql.append(" and _dailyRecord.drMrno like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrMrno()));
	   	}		
	   	if (queryVo.getDrPatientname() != null && !"".equals(queryVo.getDrPatientname())) {
	   		jpql.append(" and _dailyRecord.drPatientname like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrPatientname()));
	   	}		
	   	if (queryVo.getDrIndate() != null) {
	   		jpql.append(" and _dailyRecord.drIndate between ? and ? ");
	   		conditionVals.add(queryVo.getDrIndate());
	   		conditionVals.add(queryVo.getDrIndateEnd());
	   	}	
	   	if (queryVo.getDrOutdate() != null) {
	   		jpql.append(" and _dailyRecord.drOutdate between ? and ? ");
	   		conditionVals.add(queryVo.getDrOutdate());
	   		conditionVals.add(queryVo.getDrOutdateEnd());
	   	}	
	   	if (queryVo.getDrPatienttype() != null && !"".equals(queryVo.getDrPatienttype())) {
	   		jpql.append(" and _dailyRecord.drPatienttype like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrPatienttype()));
	   	}		
	   	if (queryVo.getDrIndep() != null && !"".equals(queryVo.getDrIndep())) {
	   		jpql.append(" and _dailyRecord.drIndep like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrIndep()));
	   	}		
	   	if (queryVo.getDrOutdep() != null && !"".equals(queryVo.getDrOutdep())) {
	   		jpql.append(" and _dailyRecord.drOutdep like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrOutdep()));
	   	}		
	   	if (queryVo.getDrDrugcost() != null) {
	   		jpql.append(" and _dailyRecord.drDrugcost=?");
	   		conditionVals.add(queryVo.getDrDrugcost());
	   	}	
	   	if (queryVo.getDrNodrugcost() != null) {
	   		jpql.append(" and _dailyRecord.drNodrugcost=?");
	   		conditionVals.add(queryVo.getDrNodrugcost());
	   	}	
	   	if (queryVo.getDrTotalcost() != null) {
	   		jpql.append(" and _dailyRecord.drTotalcost=?");
	   		conditionVals.add(queryVo.getDrTotalcost());
	   	}	
	   	if (queryVo.getDrIsabnormal() != null) {
	   		jpql.append(" and _dailyRecord.drIsabnormal=?");
	   		conditionVals.add(queryVo.getDrIsabnormal());
	   	}	
	   	if (queryVo.getDrInhisdep() != null && !"".equals(queryVo.getDrInhisdep())) {
	   		jpql.append(" and _dailyRecord.drInhisdep like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrInhisdep()));
	   	}		
	   	if (queryVo.getDrOuthisdep() != null && !"".equals(queryVo.getDrOuthisdep())) {
	   		jpql.append(" and _dailyRecord.drOuthisdep like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDrOuthisdep()));
	   	}		
        Page<DailyRecord> pages = getQueryChannelService()
		   .createJpqlQuery(jpql.toString())
		   .setParameters(conditionVals)
		   .setPage(currentPage, pageSize)
		   .pagedList();
		   
        return new Page<DailyRecordDTO>(pages.getStart(), pages.getResultCount(),pageSize, DailyRecordAssembler.toDTOs(pages.getData()));
	}

	
	/**
     * @methodname    :daysParserXml
     * @description   :根据传入的日期，解析xml ，存储DailyRecord
     * @param         :str，传递过来的xml字符串 
     * @author        :WangTeng
     * @return        :
     * @date          :2014年9月16日
     */
	@Override
    public void daysParserXml(String day) {
        //定时任务使用java反射时必须重新实例化该接口，bean括号里面填impl实例上面的@Name括号里面的内容
        if(departmentApplication==null){
        	departmentApplication =(DepartmentApplication) ContextLoader.getCurrentWebApplicationContext().getBean("departmentApplicationImplqiaoyilong20181026");
        }
    	List<Department> depList = departmentApplication.findAllDepartment();
    	Map<String, String> depHisdep = new HashMap<String, String>();//HIS科室名和本地库科室名map
    	for (Department dep : depList) {
			//depHisdep.put(dep.getDepHis(), dep.getDepName());
			if (!StringUtils.isEmpty(dep.getDepHis())) {
				String[] deps = dep.getDepHis().split(",");
				for (String his : deps) {
					//System.out.println("[dayMonitor]   "+his);
					depHisdep.put(his, dep.getDepName()+","+dep.getParHospCode());
				}
			}
		}
    	String dayMonitor = "";
    	String dayMonitorDep = "";
		String wsUrl = "";//日监测地址
		String method = "";//日监测方法
		String sp_dep1 = "";
		String sp_dep2 = "";
		String sp_dep3 = "";
		List<String> spDepList1 =new LinkedList<String>();
		List<String> spDepList2 =new LinkedList<String>();
		List<String> spDepList3 = new LinkedList<String>();
		
		//取配置的公共方法--读取周监测接口地址配置
		 dayMonitor=GlobalData.sysConfig.get("DAY_MONITOR");
		if(StringUtils.hasLength(dayMonitor)&&dayMonitor.split("θ").length==2){
			wsUrl = dayMonitor.split("θ")[0];
			method = dayMonitor.split("θ")[1];
		}
		
		//取配置的公共方法--读取设置好的特殊科室配置
		dayMonitorDep=GlobalData.sysConfig.get("DAYMONITOR_DEP");
		if(StringUtils.hasLength(dayMonitorDep)&&dayMonitorDep.split("θ").length>=3){
			sp_dep1 = dayMonitorDep.split("θ")[0];
			sp_dep2 = dayMonitorDep.split("θ")[1];
			sp_dep3 = dayMonitorDep.split("θ")[2];
		}
		//将获取到的特殊科室配置根据分隔符分隔，第一类是不能出院的科室，如ICU，第二类是标准科室中没有，但是又合理的科室，如妇产科
		//并将两类科室通过list存储
		if(!sp_dep1.isEmpty())
		{
			
			spDepList1 = Arrays.asList(sp_dep1.split("&")); 
		}
		if(!sp_dep2.isEmpty())
		{
			
			spDepList2 = Arrays.asList(sp_dep2.split("&")); 
		}
		if(!sp_dep3.isEmpty())
		{
			
			spDepList3 = Arrays.asList(sp_dep3.split("&")); 
		}
	
		//动态调用
		JaxWsDynamicClientFactory factory=JaxWsDynamicClientFactory.newInstance();
//		org.apache.cxf.endpoint.Client client=factory.createClient("http://192.168.2.18/trakcare/web/DRGS.MonitorEveryDay.CLS?WSDL=1");
		org.apache.cxf.endpoint.Client client=factory.createClient(wsUrl);
		
		//日期格式
    	SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");

		String xml="";//xml字符串
//		xml="<Response><AdmDATA><PatientID>111111</PatientID><PatientNo>0000000</PatientNo>"
//				+ "<ADMRowid>11111111</ADMRowid><PatientName>董光宇</PatientName><SocialStatus>外地医保</SocialStatus>"
//				+ "<RDesc>肿瘤科</RDesc><PatDep>肿瘤科</PatDep><AdmDate>2018-07-23</AdmDate><DischDate>2018-9-1</DischDate>"
//				+ "<AllOrdFee>382403.09</AllOrdFee><DrugOrdFee>182257.42</DrugOrdFee><QTOrdFee>200145.67</QTOrdFee>"
//				
//				+ "</AdmDATA><AdmDATA><PatientID>111112</PatientID><PatientNo>0257</PatientNo>"
//				+ "<ADMRowid>22222222</ADMRowid><PatientName>陈佳辉</PatientName><SocialStatus>外地医保</SocialStatus>"
//				+ "<RDesc>眼科</RDesc><PatDep>ICU</PatDep><AdmDate>2018-07-30</AdmDate><DischDate>2018-9-1</DischDate>"
//				+ "<AllOrdFee>382403.09</AllOrdFee><DrugOrdFee>182257.42</DrugOrdFee><QTOrdFee>200145.67</QTOrdFee></AdmDATA>"
//				
//				+ "<AdmDATA><PatientID>111113</PatientID><PatientNo>057518</PatientNo><ADMRowid>33333333</ADMRowid>"
//				+ "<PatientName>陈振鹏</PatientName><SocialStatus>外地医保</SocialStatus><RDesc>妇产科</RDesc>"
//				+ "<PatDep>妇产科</PatDep><AdmDate>2018-08-04</AdmDate><DischDate>2018-9-1</DischDate>"
//				+ "<AllOrdFee>382403.09</AllOrdFee><DrugOrdFee>182257.42</DrugOrdFee><QTOrdFee>200145.67</QTOrdFee>"
//				+ "</AdmDATA></Response>";
		try {
//			Object[] objects=client.invoke("DischDateAdmFeeIndex",day);
			Object[] objects=client.invoke(method,day);
			xml = (String) objects[0];
		} catch (Exception e) {
			e.printStackTrace();
		}	
    	
		//替换xml里的“--@%Library.GlobalCharacterStream”
		xml=xml.replaceAll("\\d\\d@%Library.GlobalCharacterStream", "");
		
		String inHisDep="";//入院his科室
		String outHisDep="";//出院his科室
		try {
			Map<String ,Object> paramMap=new HashMap<>();
			Document document = DocumentHelper.parseText(xml);
			Element root = document.getRootElement();//获取根节点
			String rootName = root.getName();//根节点名称
	        //定时任务使用java反射时必须重新实例化该接口，bean括号里面填impl实例上面的@Name括号里面的内容
	        if(xmlStructureApplication==null){
	        	xmlStructureApplication =(XmlStructureApplication) ContextLoader.getCurrentWebApplicationContext().getBean("xmlStructureApplicationImplqiaoyilong20181026");
	        }
			//获取存储好的周监测xml模板
			List<Object[]> xmlStrucList = xmlStructureApplication.getXmlStructureByParam("dayMonitorRecord","","","");
			String caseNodeName = "";//单病例节点的名称
			
			//xml中有一条病例
			if(!xmlStrucList.isEmpty()){
				caseNodeName = xmlStrucList.get(0)[3].toString();
				if(caseNodeName.startsWith("/"+rootName)){
					paramMap.put("pathPrefix","");
					paramMap.put("curNodeName","");
					paramMap.put("num","");
					//将节点信息存入对应的实体字段中
					Map<String,Object> map =  parseXmlNode(root,paramMap);
					//取出实体，该实体已经含有xml的解析信息
					DailyRecord dailyRecord=(DailyRecord) map.get("dailyRecord");
					/************************以下为业务操作 省略********************************/
					/***1.取his科室做科室对照
					 * 2. 对特殊科室进行判断
					 * 3. 标记是否异常
					 * ***/
					dailyRecord.setDr_is_abnormal((double)1);//先将所有数据都设置为异常数据
					inHisDep = dailyRecord.getDr_in_hisdep();//获取his科室名
					outHisDep = dailyRecord.getDr_out_hisdep();
					dailyRecord.setDr_in_dep(inHisDep);//将his科室名赋给出入院科室名
					dailyRecord.setDr_out_dep(outHisDep);
					
					String inDep = depHisdep.get(inHisDep);//根据his名获取科室名
					String outDep = depHisdep.get(outHisDep);
					
					  if (inDep!=null && outDep!=null) {
		                	//出入院都正常，标为正常
					    	dailyRecord.setDr_in_dep(inDep.split(",")[0]);
							dailyRecord.setDr_out_dep(outDep.split(",")[0]);
							dailyRecord.setDr_hospcode(outDep.split(",")[1]!=null?outDep.split(",")[1]:"");
							dailyRecord.setDr_is_abnormal((double)0);
						}else if(inDep!=null && outDep==null){
							dailyRecord.setDr_in_dep(inDep.split(",")[0]);
							if(spDepList1.contains(outHisDep)){
							   //入院正常，出院为sicu和ICU时，将出院科室改为入院科室，标为正常
								dailyRecord.setDr_out_dep(inDep.split(",")[0]);
								dailyRecord.setDr_hospcode(inDep.split(",")[1]!=null?inDep.split(",")[1]:"");
								dailyRecord.setDr_is_abnormal((double)0);
							}else if (spDepList2.contains(outHisDep)){
								//入院正常，出院为妇产科和综合科时，标为正常
								dailyRecord.setDr_is_abnormal((double)0);
								dailyRecord.setDr_hospcode(inDep.split(",")[1]!=null?inDep.split(",")[1]:"");
							}else {
								//其余入院正常，出院异常的全部标为异常
								dailyRecord.setDr_is_abnormal((double)1);
							}
						}else if(inDep==null && outDep!=null){	
							dailyRecord.setDr_out_dep(outDep.split(",")[0]);
							dailyRecord.setDr_hospcode(outDep.split(",")[1]!=null?outDep.split(",")[1]:"");
							if(spDepList1.contains(inHisDep)){
							//入院正常，出院为sicu和ICU时，将出院科室改为入院科室，标为正常
								dailyRecord.setDr_in_dep(outDep);
								dailyRecord.setDr_is_abnormal((double)0);
							}else if (spDepList2.contains(inHisDep)){
								//出院正常，入院为妇产科和综合科时，标为正常
								dailyRecord.setDr_is_abnormal((double)0);
							}else {
								//其余出院正常，入院异常的全部标为异常
								dailyRecord.setDr_is_abnormal((double)1);
							}
						}else {
							  for(int j=0;j<spDepList2.size();j++){
								  //获取第二类特殊科室进行判断
								  String outDepTemp = spDepList2.get(j);
								  if (outHisDep.equals(outDepTemp) && inHisDep.equals(outDepTemp)) {
									  dailyRecord.setDr_is_abnormal((double)0);
									  for(int i=0;i<spDepList3.size();i++){
										  String dep2 = spDepList3.get(i).split(":")[0];//第二类科室
										  String dep3 = spDepList3.get(i).split(":")[1];//第二类科室的对照科室
										  if(dep2.equals(outDepTemp) && dep3 != null){
											  outDep = depHisdep.get(dep3.split(",")[0]);
											  dailyRecord.setDr_hospcode(outDep.split(",")[1]!=null?outDep.split(",")[1]:"");
										  }
									  }
								  }else{
									  dailyRecord.setDr_is_abnormal((double)1);
								  }
							  }
							
						}
				        //定时任务使用java反射时必须重新实例化该接口，bean括号里面填impl实例上面的@Name括号里面的内容
				        if(application==null){
				        	application =(DailyRecordApplication) ContextLoader.getCurrentWebApplicationContext().getBean("dailyRecordApplicationImplqiaoyilong20181026");
				        }
					    application.creatDailyRecord(dailyRecord);
				    
				}else{//xml中有多条病例
					
					List<Node> nodeChild = root.selectNodes("/"+rootName+"/"+caseNodeName.split("/")[1]);
					for(int i = 1;i<=nodeChild.size();i++){
						paramMap.put("pathPrefix","/"+rootName);
						paramMap.put("curNodeName",nodeChild.get(i-1).getName());
						paramMap.put("num","["+i+"]");
						//将节点信息存入对应的实体字段中
						Map<String,Object> map =  parseXmlNode(root,paramMap);
						//取出实体，该实体已经含有xml的解析信息
						DailyRecord dailyRecord=(DailyRecord) map.get("dailyRecord");
						/************************以下为业务操作 省略********************************/
						/***1.取his科室做科室对照
						 * 2. 对特殊科室进行判断
						 * 3. 标记是否异常
						 * ***/
						dailyRecord.setDr_is_abnormal((double)1);
						inHisDep = dailyRecord.getDr_in_hisdep();//获取his科室名
						outHisDep = dailyRecord.getDr_out_hisdep();
						dailyRecord.setDr_in_dep(inHisDep);
						dailyRecord.setDr_out_dep(outHisDep);
						
						String inDep = depHisdep.get(inHisDep);//根据his名获取科室名
						String outDep = depHisdep.get(outHisDep);
					    if (inDep!=null && outDep!=null) {
		                	//出入院都正常，标为正常
					    	dailyRecord.setDr_in_dep(inDep);
							dailyRecord.setDr_out_dep(outDep);
							dailyRecord.setDr_hospcode(outDep.split(",")[1]!=null?outDep.split(",")[1]:"");
							dailyRecord.setDr_is_abnormal((double)0);
						}else if(inDep!=null && outDep==null){
							dailyRecord.setDr_in_dep(inDep);
							if(spDepList1.contains(outHisDep)){
							   //入院正常，出院为sicu和ICU时，将出院科室改为入院科室，标为正常
								dailyRecord.setDr_out_dep(inDep);
								dailyRecord.setDr_hospcode(inDep.split(",")[1]!=null?inDep.split(",")[1]:"");
								dailyRecord.setDr_is_abnormal((double)0);
							}else if (spDepList2.contains(outHisDep)){
								//入院正常，出院为妇产科和综合科时，标为正常
								dailyRecord.setDr_is_abnormal((double)0);
								dailyRecord.setDr_hospcode(inDep.split(",")[1]!=null?inDep.split(",")[1]:"");
							}else {
								//其余入院正常，出院异常的全部标为异常
								dailyRecord.setDr_is_abnormal((double)1);
							}
						}else if(inDep==null && outDep!=null){	
							dailyRecord.setDr_out_dep(outDep);
							dailyRecord.setDr_hospcode(outDep.split(",")[1]!=null?outDep.split(",")[1]:"");
							if(spDepList1.contains(inHisDep)){
							//入院正常，出院为sicu和ICU时，将出院科室改为入院科室，标为正常
								dailyRecord.setDr_in_dep(outDep);
								dailyRecord.setDr_is_abnormal((double)0);
							}else if (spDepList2.contains(inHisDep)){
								//出院正常，入院为妇产科和综合科时，标为正常
								dailyRecord.setDr_is_abnormal((double)0);
							}else {
								//其余出院正常，入院异常的全部标为异常
								dailyRecord.setDr_is_abnormal((double)1);
							}
						}else {
								  if (spDepList2.contains(outHisDep)&&spDepList2.contains(inHisDep)){
									  dailyRecord.setDr_is_abnormal((double)0);
								  }else{
									  dailyRecord.setDr_is_abnormal((double)1);
								  }
							
						}
					    
							try{
								if(dailyRecord.getDr_episode_id()==null) {
									logger.error("时间："+new Date()+"--数据："+dailyRecord.getDr_mr_no()+"-"+dailyRecord.getDr_out_hisdep()+"没有主键");
									return;
								}else{
							        //定时任务使用java反射时必须重新实例化该接口，bean括号里面填impl实例上面的@Name括号里面的内容
							        if(application==null){
							        	application =(DailyRecordApplication) ContextLoader.getCurrentWebApplicationContext().getBean("dailyRecordApplicationImplqiaoyilong20181026");
							        }
									application.creatDailyRecord(dailyRecord);		
								}
							}catch(Exception e){
								e.printStackTrace();
								
							}
					}
				}
			}
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
        /*try {
            StringReader read = new StringReader(xml);
            // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入  
            InputSource source = new InputSource(read);
            // 创建一个新的SAXBuilder  
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(source);
            //根节点
            Node rootNode = document.getChildNodes().item(0);
            NodeList nodeList=rootNode.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
            	//创建并初始化实体类DailyRecord
//            	DailyRecord dailyRecord=new DailyRecord("0", (double)0, 
//            			new Date(), "0", "0", BigDecimal.valueOf(0), "0", 
//            			BigDecimal.valueOf(0), new Date(), "0", "0", "0", "0", 
//            			BigDecimal.valueOf(0),"0","0");
            	Node node = nodeList.item(i);
            	if(node.getNodeType()!=Node.ELEMENT_NODE)
				{
					continue;
				}
            	DailyRecord dailyRecord = new DailyRecord();
                for (int j = 0; j < node.getChildNodes().getLength(); j++) {
                	//获取子节点
                    Node childNode = node.getChildNodes().item(j);
                    if (childNode.getNodeType() != Node.ELEMENT_NODE||!childNode.hasChildNodes())
    				{
    					continue;
    				}
                    //节点名
                    String nodeName = childNode.getNodeName();
                    //节点值
                    String nodeValue = childNode.getFirstChild().getNodeValue();
                    //循环子节点
                    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                        if ("PatientID".equals(nodeName)) {
                        	//病案号---其实是登记号
                        	//dailyRecord.setDrMrNo(nodeValue);
                        } else if ("PatientNo".equals(nodeName)) {
                        	//登记号---其实是病案号
                        	System.out.println("登记号："+nodeValue);
                        	dailyRecord.setDrMrno(nodeValue);
                        } else if ("ADMRowid".equals(nodeName)) {
                        	//就诊ID
                        	dailyRecord.setDrEpisodeid(nodeValue);
                        } else if ("PatientName".equals(nodeName)) {
                        	//病人姓名
                        	dailyRecord.setDrPatientname(nodeValue);  
                        } else if ("SocialStatus".equals(nodeName)) {
                        	//病人类型
                        	dailyRecord.setDrPatienttype(nodeValue);  
                        } else if ("RDesc".equals(nodeName)) {
                        	//入院his科室
                        	dailyRecord.setDrInhisdep(nodeValue);
                        	dailyRecord.setDrIndep(nodeValue);
                        	inHisDep=nodeValue;
                        } else if ("PatDep".equals(nodeName)) {
                        	//出院his科室
                        	dailyRecord.setDrOuthisdep(nodeValue);
                        	dailyRecord.setDrOutdep(nodeValue);
                        	outHisDep=nodeValue;
                        }  else if ("AdmDate".equals(nodeName)) {
                        	//入院日期
                        	dailyRecord.setDrIndate(dateFormat.parse(nodeValue));
                        } else if ("DischDate".equals(nodeName)) {
                        	//出院日期
                        	dailyRecord.setDrOutdate(dateFormat.parse(nodeValue));  
                        } else if ("AllOrdFee".equals(nodeName)) {
                        	//总费用
                        	dailyRecord.setDrTotalcost(Double.valueOf(nodeValue));  
                        }  else if ("DrugOrdFee".equals(nodeName)) {
                        	//药品费用
                        	dailyRecord.setDrDrugcost(Double.valueOf(nodeValue));  
                        } else if ("QTOrdFee".equals(nodeName)) {
                        	//非药品费用
                        	dailyRecord.setDrNodrugcost(Double.valueOf(nodeValue));  
                        }
                        else if ("AdmDocName".equals(nodeName)) {
                        	//主治医师
                        	dailyRecord.setDrProtoMedicus(nodeValue);  
                        }else if ("AdmDocGP".equals(nodeName)) {
                        	//主诊医师
                        	dailyRecord.setDrGroupName(nodeValue);  
                        }
                    }
                }
                
//                //修改PICU为小儿心脏科
//                if (inHisDep.equals("PICU")) {
//					dailyRecord.setDrInDep("小儿心脏科");
//				}
//                if (outHisDep.equals("PICU")) {
//					dailyRecord.setDrOutDep("小儿心脏科");
//				}
//                //修改PICU为小儿心脏科
//                if (inHisDep.equals("picu")) {
//					dailyRecord.setDrInDep("小儿心脏科");
//				}
//                if (outHisDep.equals("picu")) {
//					dailyRecord.setDrOutDep("小儿心脏科");
//				}
//                
//                //判断是否异常
//                List<Map<String, Object>> inDep=dayMonitorService.findDepByName(dailyRecord.getDrInDep());
//                List<Map<String, Object>> outDep=dayMonitorService.findDepByName(dailyRecord.getDrOutDep());
//                //修改科室名
//                if (outDep.size()>0) {
//                	//出院都正常，标为正常
//                	dailyRecord.setDrOutDep(outDep.get(0).get("DEP_NAME").toString());
//					dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//				}else if(inDep.size()>0 && outDep.size()==0){
//				
//					if (outHisDep.equals("sicu")||outHisDep.equals("ICU")) {
//						//入院正常，出院为sicu和ICU时，将出院科室改为入院科室（非his科室），标为正常
//						dailyRecord.setDrOutDep(inDep.get(0).get("DEP_NAME").toString());
//						dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//					}else if (outHisDep.equals("妇产科")||outHisDep.equals("综合科")) {
//						//入院正常，出院为妇产科和综合科时，标为正常
//						dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//					}else {
//						//其余入院正常，出院异常的全部标为异常
//						dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(1));
//					}
//					
//				}else {
//					if (outHisDep.equals("妇产科")||outHisDep.equals("综合科")) {
//						//入院正常，出院为妇产科和综合科时，标为正常
//						dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//					}else{
//					//出入院都异常的标为异常
//					dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(1));
//					}
//				}
//===============================================================================
                //修改PICU为小儿心脏科
                if (inHisDep.equals("PICU")) {
					dailyRecord.setDrIndep("小儿心脏科");
				}
                if (outHisDep.equals("PICU")) {
					dailyRecord.setDrOutdep("小儿心脏科");
				}
              //修改PICU为小儿心脏科
              if (inHisDep.equals("picu")) {
					dailyRecord.setDrIndep("小儿心脏科");
				}
              if (outHisDep.equals("picu")) {
					dailyRecord.setDrOutdep("小儿心脏科");
				}
                //判断是否异常
				String inDep = depHisdep.get(dailyRecord.getDrIndep());
				String outDep = depHisdep.get(dailyRecord.getDrOutdep());
                //修改科室名
                if (inDep!=null && outDep!=null) {
                	//出入院都正常，标为正常
                	dailyRecord.setDrIndep(inDep);
                	dailyRecord.setDrOutdep(outDep);
					dailyRecord.setDrIsabnormal((double)0);
				}else if(inDep!=null && outDep==null){
					dailyRecord.setDrIndep(inDep);
					if (outHisDep.equals("sicu")||outHisDep.equals("ICU")||outHisDep.equals("icu")||outHisDep.equals("SICU")) {
						//入院正常，出院为sicu和ICU时，将出院科室改为入院科室，标为正常
						dailyRecord.setDrOutdep(dailyRecord.getDrIndep());
						dailyRecord.setDrIsabnormal((double)0);
					}else if (outHisDep.equals("妇产科")||outHisDep.equals("综合科")) {
						//入院正常，出院为妇产科和综合科时，标为正常
						dailyRecord.setDrIsabnormal((double)0);
					}else {
						//其余入院正常，出院异常的全部标为异常
						dailyRecord.setDrIsabnormal((double)1);
					}
				}else if(inDep==null && outDep!=null){
					dailyRecord.setDrOutdep(outDep);
					if (inHisDep.equals("妇产科")||inHisDep.equals("综合科")) {
						//出院正常，入院为妇产科和综合科时，标为正常
						dailyRecord.setDrIsabnormal((double)0);
					}else {
						//其余出院正常，入院异常的全部标为异常
						dailyRecord.setDrIsabnormal((double)1);
					}
				}else {
				       if (outHisDep.equals("妇产科") && inHisDep.equals("妇产科")) {
		                	//出入院都为妇产科是标为正常
							dailyRecord.setDrIsabnormal((double)0);
						} else if (outHisDep.equals("综合科") && inHisDep.equals("综合科")) {
		                	//出入院都为综合科是标为正常
							dailyRecord.setDrIsabnormal((double)0);
						}else {
							//出入院都异常的标为异常
							dailyRecord.setDrIsabnormal((double)1);
						}
					
				}
 //==============================================================================
//                //修改科室名
//                if (inDep.size()>0 && outDep.size()>0) {
//                	//出入院都正常，标为正常
//                	dailyRecord.setDrInDep(inDep.get(0).get("DEP_NAME").toString());
//                	dailyRecord.setDrOutDep(outDep.get(0).get("DEP_NAME").toString());
//					dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//				}else if(inDep.size()>0 && outDep.size()==0){
//					dailyRecord.setDrInDep(inDep.get(0).get("DEP_NAME").toString());
//					if (outHisDep.equals("sicu")||outHisDep.equals("ICU")) {
//						//入院正常，出院为sicu和ICU时，将出院科室改为入院科室，标为正常
//						dailyRecord.setDrOutDep(dailyRecord.getDrInDep());
//						dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//					}else if (outHisDep.equals("妇产科")||outHisDep.equals("综合科")) {
//						//入院正常，出院为妇产科和综合科时，标为正常
//						dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//					}else {
//						//其余入院正常，出院异常的全部标为异常
//						dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(1));
//					}
//					
//				}else if(inDep.size()==0 && outDep.size()>0){
//					dailyRecord.setDrOutDep(outDep.get(0).get("DEP_NAME").toString());
//					if (inHisDep.equals("妇产科")||inHisDep.equals("综合科")) {
//						//出院正常，入院为妇产科和综合科时，标为正常
//						dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//					}else {
//						//其余出院正常，入院异常的全部标为异常
//						dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(1));
//					}
//				}else {
//					//出入院都异常的标为异常
//					dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(1));
//				}
//                
//                if (outHisDep.equals("妇产科") && inHisDep.equals("妇产科")) {
//                	//出入院都为妇产科是标为正常
//					dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//				}
//                if (outHisDep.equals("综合科") && inHisDep.equals("综合科")) {
//                	//出入院都为综合科是标为正常
//					dailyRecord.setDrIsAbnormal(BigDecimal.valueOf(0));
//				}
                
                //存储
                application.creatDailyRecord(dailyRecord);	
            }
        } catch (FileNotFoundException e) {  
            System.out.println(e.getMessage());  
        } catch (ParserConfigurationException e) {  
            System.out.println(e.getMessage());  
        } catch (SAXException e) {  
            System.out.println(e.getMessage());  
        } catch (IOException e) {  
            System.out.println(e.getMessage());  
        } catch (ParseException e) {
        	System.out.println(e.getMessage());
		}  */
    }
    /**
	 * @methodname    :importDayData
	 * @description   :导入昨天日监测数据
	 * @author        :WangTeng
	 * @date          :2015年7月27日
	 */
	@Override
	public void importDayData() {
		//定时任务自动更新
		System.out.println("---- 自动任务导入日监测数据  "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" ----");
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
    	Calendar calendar=Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String yesterday=dateFormat.format(calendar.getTime());//昨天日期
		daysParserXml(yesterday);
	}
    /**
	 * @methodname    :importDayData
	 * @description   :导入昨天日监测数据,定时任务调用转接，以QuartzJob结尾
	 * @author        :WangTeng
	 * @date          :2015年7月27日
	 * cron 表达式  0 0 7 * * ? 每天凌晨七点整导入昨天日监测数据
	 */
	public void importDayDataQuartzJob(String param) {
		//定时任务自动更新
		importDayData();
	}
	/**
	 * 
	 *  parseXmlNode.  
	 *  @Description：解析xml
	 *  @Author LiuHongjun
	 *  @param element
	 *  @param paramMap
	 *  @return 
	 *  @Date:2017年10月10日下午9:02:59
	 */
	 public Map<String,Object> parseXmlNode(Element element,Map<String,Object> paramMap){
		 Map<String,Object> map=new HashMap<>();
		    String pathPrefix = paramMap.get("pathPrefix").toString();
	    	String curNodeName = paramMap.get("curNodeName").toString();
	    	String num = paramMap.get("num").toString();
	    	DailyRecord dailyRecord=new DailyRecord();
	    	 Map<String,Object> filedMap=dailyRecord.dailyRecordMap();//获取字段与属性的对应关系
			List<Object[]> dailyRecordXmlStrucList = xmlStructureApplication.getXmlStructureByParam("dayMonitorRecord","T_DAILYRECORD", "", "");
			if (!dailyRecordXmlStrucList.isEmpty()) {
				for (Object[] objects : dailyRecordXmlStrucList) {
					// 获取xml路径
					String xpathBaseInfo = pathPrefix + objects[3].toString().replaceFirst(curNodeName,curNodeName+num);
					org.dom4j.Node nodeBi = element.selectSingleNode(xpathBaseInfo);
					if(nodeBi==null){
						continue;//当前节点不存在 跳过
					}
					try {
						//获取节点值
						String nodeTextVal = nodeBi.getText().toString();
						// 使用反射，调用对象实体类Set方法
						System.out.println(objects[4].toString());
						System.out.println(objects[13]);
						System.out.println(objects[12].toString());
						System.out.println(objects[5]);
						DrgsIndex.setElementValue(dailyRecord, objects[4].toString(),objects[13] == null ? "" : objects[13].toString(), objects[12].toString(), nodeTextVal,objects[5] == null ? "" : objects[5].toString(),filedMap);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}	
			}
			map.put("dailyRecord", dailyRecord);
		 return map; 
	 }


	
	 
}
